﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SisLegal.Model;

namespace SisLegal.Expedient
{
    public partial class FiscalFolderEditForm : System.Web.UI.Page
    {
        private int ID
        {
            get
            {
                if (Request.QueryString["id"] == null)
                    return 0;
                else
                    return int.Parse(Request.QueryString["id"]);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                DDL_Participation_Seal.DataSource = Enumeration.GetAll<eParticipation_Seal_Fiscal_Folder>();
                DDL_Participation_Seal.DataTextField = "Value";
                DDL_Participation_Seal.DataValueField = "Key";
                DDL_Participation_Seal.DataBind();

                DDL_Money.DataSource = Enumeration.GetAll<eMoney>();
                DDL_Money.DataTextField = "Value";
                DDL_Money.DataValueField = "Key";
                DDL_Money.DataBind();

                DDL_Type_Process.DataSource = Enumeration.GetAll<eType_Process_Fiscal_Folder>();
                DDL_Type_Process.DataTextField = "Value";
                DDL_Type_Process.DataValueField = "Key";
                DDL_Type_Process.DataBind();

                DDL_Contingency_Rate.DataSource = Enumeration.GetAll<eContingency_Rate>();
                DDL_Contingency_Rate.DataTextField = "Value";
                DDL_Contingency_Rate.DataValueField = "Key";
                DDL_Contingency_Rate.DataBind();

                DDL_Assigned_Person.DataSource = Enumeration.GetAll<eAssigned_Person>();
                DDL_Assigned_Person.DataTextField = "Value";
                DDL_Assigned_Person.DataValueField = "Key";
                DDL_Assigned_Person.DataBind();

                DDL_Physical_Location_Files.DataSource = Enumeration.GetAll<ePhysical_Location_Files>();
                DDL_Physical_Location_Files.DataTextField = "Value";
                DDL_Physical_Location_Files.DataValueField = "Key";
                DDL_Physical_Location_Files.DataBind();


                LawyerManager lawyer = new LawyerManager();
                DDL_Lawyer.DataSource = lawyer.GetAll();
                DDL_Lawyer.DataTextField = "Name";
                DDL_Lawyer.DataValueField = "ID";
                DDL_Lawyer.DataBind();
                //_---------------------------------------------

                FiscalFolderManager fiscalFolder = new FiscalFolderManager();
                SisLegal.Model.FiscalFolder fiscal = fiscalFolder.GetById(ID);
                txtNroFiscalFolder.Text = fiscal.NroFiscalFolder;
                txtYear.Text = fiscal.Year.ToString();
                txtFiscalia.Text = fiscal.Fiscalia;
                txtFiscalCargo.Text = fiscal.FiscalCargo;
                txtPoliciaCargo.Text = fiscal.PoliciaCargo;
                txtDenunciante.Text = fiscal.Denunciante;
                txtDenunciado.Text = fiscal.Denunciado;
                txtAgraviado.Text = fiscal.Agraviado;
                DDL_Participation_Seal.SelectedIndex = fiscal.ParticipationSeal;
                txtDocumentoInicial.Text = fiscal.DocumentoInicial;
                txtFechaHechos.Text = String.Format("{0:dd/MM/yyyy}", fiscal.FechaHechos);
                txtFechaNoticicacionSeal.Text = String.Format("{0:dd/MM/yyyy}", fiscal.FechaNoticicacionSeal);
                txtArticle.Text = fiscal.Article.ToString();
                txtMontoPretension.Text = fiscal.MontoPretension.ToString();
                DDL_Money.SelectedIndex = fiscal.Money;

                txtFechaInicio.Text = String.Format("{0:dd/MM/yyyy}", fiscal.FechaInicio);
                DDL_Type_Process.SelectedIndex = fiscal.TypeProcess;
                DDL_Contingency_Rate.SelectedIndex = fiscal.ContingencyRate;
                txtSumilla.Text = fiscal.Sumilla;
                txtObservaciones.Text = fiscal.Observaciones;

                DDL_Assigned_Person.SelectedIndex = fiscal.AssignedPerson;
                DDL_Lawyer.SelectedIndex = fiscal.Lawyer;
                txtOrdenServicio.Text = fiscal.OrdenServicio;
                txtNroRecibo.Text = fiscal.NroRecibo;
                txtTerminosReferencia.Text = fiscal.TerminosReferencia;
                txtAbogadoSeal.Text = fiscal.AbogadoSeal;
                txtPracticanteSeal.Text = fiscal.PracticanteSeal;
                DDL_Physical_Location_Files.SelectedIndex = fiscal.PhysicalLocationFiles;
            }
        }
        protected void btnSearchCrime_Click(object sender, EventArgs e)
        {
            CrimeSpecificManager crimeManager = new CrimeSpecificManager();
            CrimeSpecific crime = crimeManager.GetByArticle(Convert.ToInt32(txtArticle.Text));
            txtArticleDetail1.Text = crime.NameGenerico;
            txtArticleDetail2.Text = crime.NameSubGenerico;
            txtArticleDetail3.Text = crime.Name;
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            Model.FiscalFolderManager typeProcessManager = new Model.FiscalFolderManager();
            Model.FiscalFolder fiscalFolder = new Model.FiscalFolder();

            if (txtYear.Text == "") { txtYear.Text = "0"; }
            if (txtFechaHechos.Text == "") { txtFechaHechos.Text = "0001-01-01"; }
            if (txtFechaNoticicacionSeal.Text == "") { txtFechaNoticicacionSeal.Text = "0001-01-01"; }
            if (txtFechaInicio.Text == "") { txtFechaInicio.Text = "0001-01-01"; }
            if (txtMontoPretension.Text == "") { txtMontoPretension.Text = "0"; }
            if (txtArticle.Text == "") { txtArticle.Text = "0"; }



            fiscalFolder.NroFiscalFolder = txtNroFiscalFolder.Text;
            fiscalFolder.Year = Convert.ToInt32(txtYear.Text);
            fiscalFolder.Fiscalia = txtFiscalia.Text;
            fiscalFolder.FiscalCargo = txtFiscalCargo.Text;
            fiscalFolder.FechaHechos = Convert.ToDateTime(txtFechaHechos.Text);
            fiscalFolder.FechaNoticicacionSeal = Convert.ToDateTime(txtFechaNoticicacionSeal.Text);
            fiscalFolder.FechaInicio = Convert.ToDateTime(txtFechaInicio.Text);
            fiscalFolder.MontoPretension = Convert.ToDecimal(txtMontoPretension.Text);
            fiscalFolder.ParticipationSeal = Convert.ToInt32(DDL_Participation_Seal.SelectedItem.Value);
            fiscalFolder.Article = Convert.ToInt32(txtArticle.Text);
            fiscalFolder.Money = Convert.ToInt32(DDL_Money.SelectedItem.Value);
            fiscalFolder.TypeProcess = Convert.ToInt32(DDL_Type_Process.SelectedItem.Value);
            fiscalFolder.ContingencyRate = Convert.ToInt32(DDL_Contingency_Rate.SelectedItem.Value);
            fiscalFolder.PoliciaCargo = txtPoliciaCargo.Text;
            fiscalFolder.Denunciante = txtDenunciante.Text;
            fiscalFolder.Denunciado = txtDenunciado.Text;
            fiscalFolder.Agraviado = txtAgraviado.Text;
            fiscalFolder.DocumentoInicial = txtDocumentoInicial.Text;
            fiscalFolder.Sumilla = txtSumilla.Text;
            fiscalFolder.Observaciones = txtObservaciones.Text;
            //---------------------------------------------
            fiscalFolder.AssignedPerson = Convert.ToInt32(DDL_Assigned_Person.SelectedItem.Value);
            fiscalFolder.Lawyer = Convert.ToInt32(DDL_Lawyer.SelectedItem.Value);
            fiscalFolder.OrdenServicio = txtOrdenServicio.Text;
            fiscalFolder.NroRecibo = txtNroRecibo.Text;
            fiscalFolder.TerminosReferencia = txtTerminosReferencia.Text;
            fiscalFolder.AbogadoSeal = txtAbogadoSeal.Text;
            fiscalFolder.PracticanteSeal = txtPracticanteSeal.Text;
            fiscalFolder.PhysicalLocationFiles = Convert.ToInt32(DDL_Physical_Location_Files.SelectedItem.Value);




            int newArticleID = typeProcessManager.Save(fiscalFolder);

            if (newArticleID != 0)
            {
                //lblConfirmation.Text = "Grabado Satifactoriamente";
                //lblConfirmation.Visible = true;
            }
            else
            {
                //lblConfirmation.Text = "Error en la Grabacion";
                //lblConfirmation.Visible = true;
            }

            ClearForm();
        }

        private void ClearForm()
        {
            //txtName.Text = String.Empty;
        }
    }
}